package com.juzi.wopu.web;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.juzi.wopu.domain.SysCode;
import com.juzi.wopu.domain.SysRole;
import com.juzi.wopu.domain.SysRoleFun;
import com.juzi.wopu.domain.SysRoleFunResponse;
import com.juzi.wopu.query.PageList;
import com.juzi.wopu.query.SysRoleFunQuery;
import com.juzi.wopu.service.ISysCodeService;
import com.juzi.wopu.service.ISysRoleFunService;
import com.juzi.wopu.service.ISysRoleService;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

public class SysRoleFunAction extends CRUDAaction<SysRoleFun> {
	
	private static final long serialVersionUID = 1L;

	private ISysRoleFunService sysRoleFunService;
	
	private ISysCodeService sysCodeService;
	
	private ISysRoleService sysRoleService;

	private SysRoleFun sysRoleFun = new SysRoleFun();
	
	private SysRoleFunQuery baseQuery = new SysRoleFunQuery();
	
	private List<SysRoleFunResponse> data=new ArrayList<>();
	
	private PageList pageList;
	
	public void setSysRoleFunService(ISysRoleFunService sysRoleFunService) {
		this.sysRoleFunService = sysRoleFunService;
	}
	
	public void setSysCodeService(ISysCodeService sysCodeService) {
		this.sysCodeService = sysCodeService;
	}
	
	public void setSysRoleService(ISysRoleService sysRoleService) {
		this.sysRoleService = sysRoleService;
	}
	
	@Override
	protected void list() {
		List<SysRole> allRole=sysRoleService.findAllRole();
		putContext("allRole", allRole);
		Map<Long, SysRole> sysRoleMap=new HashMap<>();
		sysRoleService.getSysRoleMap(sysRoleMap, null);
		Map<Long, SysCode> map=new HashMap<>();
		sysCodeService.getSysCodeMap(map, null);//得到sName对应SysCode的map
		this.pageList = sysRoleFunService.findByQuery(baseQuery);
		
		if(pageList!=null && pageList.getRows()!=null && pageList.getRows().size()>0){
			for(int i=0;i<pageList.getRows().size();i++){
				SysRoleFun sysRoleFun=(SysRoleFun) pageList.getRows().get(i);
				SysRoleFunResponse response=new SysRoleFunResponse(sysRoleFun);
				Long iRole = sysRoleFun.getiRole();
				SysRole sysRole = sysRoleMap.get(iRole);
				
				Long ifun=sysRoleFun.getiFun();
				SysCode sysCode=map.get(ifun);
				if(sysCode==null){
					response.setFunName("");
				}else {
					String name=sysCode.getsName();
					response.setFunName(name);
				}
				
				if(sysRole==null){
					response.setRoleName("");
				}else {
					String sName = sysRole.getsName();
					response.setRoleName(sName);
				}
				//response.setFunName(map.get(sysRoleFun.getiFun()).getsName());
				data.add(response);
			}	
		}
	}

	@Override
	public String input() throws Exception {
		List<SysRole> list=sysRoleService.getAll();
		List<SysCode> allList = sysCodeService.getAll();
		List<SysCode> allFunCode=new ArrayList<>();
		if(list!=null && list.size()>0){			
			for(SysCode sysCode:allList){
				if(sysCode.getiType()==1L || sysCode.getiType()==2L){//规则不应该死这样的，换一种判断方式				
					allFunCode.add(sysCode);
				}
			}
		}
		putContext("list", list);
		putContext("allFunCode", allFunCode);
		return INPUT;
	}

	public void validateSave() {
//		if (StringUtils.isBlank(sysRoleFun.getName())) {
//			addFieldError("name", "名称不能为空");
//		}
	}

	@Override
    @InputConfig(methodName="input")
	public String save() throws Exception {
		if(id==null){
			baseQuery.setCurrentPage(Integer.MAX_VALUE);
		}
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			if (id != null) {
				sysRoleFunService.delete(id);
				out.print("{\"success\":true,\"msg\":\"删除成功\"}");
			}else{
				out.print("{\"success\":false,\"msg\":\"没有删除成功\"}");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"success\":false,\"msg\":\"异常信息:"+e.getMessage()+"\"}");
		}
		return null;
	}

	@Override
	public void prepareInput() throws Exception {
		if (id != null) {
			sysRoleFun = sysRoleFunService.get(id);
		}
	}

	@Override
	public void prepareSave() throws Exception {
		if (id == null) {
			sysRoleFun = new SysRoleFun();
		} else {
			sysRoleFun = sysRoleFunService.get(id);
		}
	}

	@Override
	public SysRoleFun getModel() {
		return sysRoleFun;
	}

	public PageList getPageList() {
		return pageList;
	}

	public SysRoleFunQuery getBaseQuery() {
		return baseQuery;
	}

	public void setBaseQuery(SysRoleFunQuery baseQuery) {
		this.baseQuery = baseQuery;
	}

	public List<SysRoleFunResponse> getData() {
		return data;
	}

	public void setData(List<SysRoleFunResponse> data) {
		this.data = data;
	}	
}